<?php

namespace App\Admin\Controllers\Dedusting;

use App\Models\DedustingRepair;
use App\Models\DedustingDevice;
use App\Http\Controllers\Controller;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Callout;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Form;
use Dcat\Admin\Admin;
use Dcat\Admin\Show;
use Illuminate\Http\Request;
use App\Handlers\IotCardHandler;

class RepairController extends Controller
{
    public function index(Content $content)
    {
        return $content
            ->header('维保记录列表')
            ->description('')
            ->body($this->grid());
    }

    protected function grid()
    {
        return new Grid(DedustingRepair::with(['device']), function (Grid $grid) {
            $grid->column('device.name', '设备');
            $grid->column('detail', '维护内容');
            $grid->column('type', '维护类型');
            $grid->column('status', '状态')->using(DedustingRepair::STATUS_TEXT);
            $grid->column('repair_at', '维护日期');
            $grid->column('created_at', '创建时间');
            $grid->column('updated_at', '修改时间');
        });
    }

    public function show($id, Content $content)
    {
        return $content->header('维保记录')
            ->description('详情')
            ->body(Show::make($id, DedustingRepair::with(['device']), function (Show $show) {
                $show->field('device.name', '设备');
                $show->field('detail', '维护内容');
                $show->field('type', '维护类型');
                $show->field('status', '状态')->using(DedustingRepair::STATUS_TEXT);
                $show->field('repair_at', '维护日期');
                $show->field('created_at', '创建时间');
                $show->field('updated_at', '修改时间');
            }));
    }

    public function update($id){
        return $this->form()->update($id);
    }
    public function store(){
        return $this->form()->store();
    }

    public function create(Content $content){
        return $content
            ->title('新建维保记录')
            ->description('创建')
            ->body($this->form());
    }

    public function form(){
        return Form::make(new DedustingRepair(), function (Form $form) {
            $devices = DedustingDevice::pluck('name','id');
            $form->select('device_id', '设备')->options($devices)->required();
            $form->text('detail', '维护内容')->required();
            $form->text('type', '维护类型')->required();
            $form->radio('status', '状态')->options(DedustingRepair::STATUS_TEXT)->default(DedustingRepair::STATUS_TODO);
            $form->datetime('repair_at', '维护日期')->format('YYYY-MM-DD HH:mm:ss')->default(null);
            $form->hidden('user_id')->value(Admin::user()->id);

            $form->saving(function (Form $form) {
                $form->user_id = Admin::user()->id;
            });
        });
    }

    public function edit($id,Content $content){
        return $content
            ->title('维保记录编辑')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    public function destroy($id)
    {
        return $this->form()->destroy($id);
    }
}